package com.ra.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ra.exception.BizException;
import com.ra.mapper.ArticleMapper;
import com.ra.pojo.Article;
import com.ra.pojo.ArticleTag;
import com.ra.pojo.Category;
import com.ra.mapper.CategoryMapper;
import com.ra.pojo.RespPageBean;

import com.ra.pojo.dto.CategoryAdminDTO;
import com.ra.pojo.dto.CategoryDTO;
import com.ra.pojo.dto.ConditionDTO;
import com.ra.service.ICategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ra.utils.BeanCopyUtil;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 今晚几点睡_Ada
 * @since 2023-03-09
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public RespPageBean<CategoryAdminDTO> listCategoriesAdmin(ConditionDTO conditionDTO) {

        PageHelper.startPage(conditionDTO.getCurrent(),conditionDTO.getSize());
        Page<CategoryAdminDTO> page = categoryMapper.listCategoriesAdmin(conditionDTO);
        return new RespPageBean<>(page.getTotal(),page.getResult());
    }

    @Override
    public List<CategoryDTO> listCategoriesBySearch(ConditionDTO conditionDTO) {
        List<Category> categoryList = categoryMapper.selectList(new LambdaQueryWrapper<Category>()
                .like(StringUtils.isNotBlank(conditionDTO.getKeywords()), Category::getCategoryName, conditionDTO.getKeywords())
                .orderByDesc(Category::getId));
        return BeanCopyUtil.copyList(categoryList, CategoryDTO.class);
    }
    @SneakyThrows
    @Override
    public void delCategories(List<Integer> categoryIds) {
        Integer count = articleMapper.selectCount(new LambdaQueryWrapper<Article>()
                .in(Article::getCategoryId, categoryIds));
        if (count > 0) {
            throw new BizException("删除失败，该分类下存在文章");
        }
        categoryMapper.deleteBatchIds(categoryIds);

    }
    @SneakyThrows
    @Override
    public void saveOrUpdateCategory(CategoryDTO categoryDTO) {
        Category existCategory = categoryMapper.selectOne(new LambdaQueryWrapper<Category>()
                .select(Category::getId)
                .eq(Category::getCategoryName, categoryDTO.getCategoryName()));
        if (Objects.nonNull(existCategory) && !existCategory.getId().equals(categoryDTO.getId())) {
            throw new BizException("分类名已存在");
        }
        Category category = BeanCopyUtil.copyObject(categoryDTO,Category.class);
        this.saveOrUpdate(category);
    }
}
